package com.hzpd.hainan.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.hzpd.hainan.app.App;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.model.usercenter.UserBean;

/**
 * Created by 猪猪侠 on 2017/6/30.
 */

public class SPUtil {
    private static SPUtil mSPutil;
    private static SharedPreferences sp;
    private static final String SETTINGS = "HZPD";
    private ACache msp;
    private UserBean user;

    private SPUtil() {
        msp = ACache.get(App.getInstance().getApplicationContext(), SETTINGS);
    }
    public static synchronized SPUtil getInstance(){
        if (mSPutil==null){
            mSPutil = new SPUtil();
        }
        if (sp==null){
            sp= App.getContext().getSharedPreferences("tongliao", Context.MODE_PRIVATE);
        }
        return mSPutil;
    }
    public String getWelcome() {
        return msp.getAsString("welcomeString");
    }

    public void setWelcome(String obj) {
        msp.put("welcomeString", obj);
    }

    public void setTextSize(int size) {
        msp.put("textsize", size + "");
    }

    public int getTextSize() {
        int textSize = CODE.textSize_normal;
        String ts = msp.getAsString("textsize");
        if (!TextUtils.isEmpty(ts)) {
            try {
                if (TextUtils.isDigitsOnly(ts)) {
                    textSize = Integer.parseInt(ts);
                }
            } catch (Exception e) {
            }
        }
        return textSize;
    }
    public void setUser(UserBean user) {
        this.user = user;
        String dsuser = FjsonUtil.toJsonString(user);
        if (TextUtils.isEmpty(dsuser)) {
            return;
        }
        String suser = null;
        try {
            suser = CipherUtils.encrypt(dsuser,
                    CipherUtils.getDESKey("fuckbitch".getBytes()), "DES");
        } catch (Exception e) {
            e.printStackTrace();
        }
        msp.put("user", suser);
    }
    public UserBean getUser() {
        if (null != user) {
            return user;
        }
        String dsuser = msp.getAsString("user");
        String suser = null;
        if (!TextUtils.isEmpty(dsuser)) {
            try {
                suser = CipherUtils.decrypt(dsuser,
                        CipherUtils.getDESKey("fuckbitch".getBytes()), "DES");
            } catch (Exception e) {
                e.printStackTrace();
            }
            user = FjsonUtil.parseObject(suser, UserBean.class);
        }
        return user;
    }

    //语言siteid
    public void setLanguage(String language) {//设置是否提醒
        sp.edit().putString("language", language).commit();
    }//是否提醒

    public String getLanguage() {
        String city = sp.getString("language", "47");
        return city;

    }
    public void setLogintype(String type){
        sp.edit().putString("logintype",type).commit();
    }
    public String getLogintype(){
        return sp.getString("logintype","2");
    }
}
